import axios from "axios";
import { ref } from 'vue'
import { defineStore } from "pinia";

export const useElementComStore = defineStore('element', () => {
  const titleList = ref([]);

  // 小站推荐
  const xztjArr = ref([]);
  //常用站点
  const cyzdArr = ref([]);
  const cyzdItems = ref([]);
  //资源搜索
  const zyssArr = ref([]);
  //软件游戏
  const rjyxArr = ref([]);
  const rjyxItems = ref([]);
  //阅读动画
  const yddhArr = ref([]);
  const yddhItems = ref([]);
  //音乐动听
  const yydtArr = ref([]);
  const yydtItems = ref([]);
  //实用工具
  const sygjArr = ref([]);
  const sygjItems = ref([]);
  //学习教育
  const xxjyArr = ref([]);
  const xxjyItems = ref([]);
  //办公素材
  const bgscArr = ref([]);
  const bgscItems = ref([]);


  //从所有数据中进行分类
  const getUrlFn = (type, arr) => {
    let list = [];
    list = arr.filter((item) => item.type == type);
    // console.log(list);
    return list;
  };

  //初始数据的筛选
  const getItemsFn = (parent, arr) => {
    console.log(parent)
    let items = [];
    items = arr.filter((item) => item.parent == parent);
    // console.log(items);
    return items;
  };

  const getUrlDataFn = async () => {
    let { data } = await axios.get("http://localhost:3000/asideList");
    let res = await axios.get("http://localhost:3000/urlList");
    xztjArr.value = getUrlFn("xztj", res.data);
    cyzdArr.value = getUrlFn("cyzd", res.data);
    zyssArr.value = getUrlFn("zyss", res.data);
    rjyxArr.value = getUrlFn("rjyx", res.data);
    yddhArr.value = getUrlFn("yddh", res.data);
    yydtArr.value = getUrlFn("yydt", res.data);
    sygjArr.value = getUrlFn("sygj", res.data);
    xxjyArr.value = getUrlFn("xxjy", res.data);
    bgscArr.value = getUrlFn("bgsc", res.data);

    //初始数据筛选
    cyzdItems.value = getItemsFn("yd2", cyzdArr.value);
    rjyxItems.value = getItemsFn("zh4", rjyxArr.value);
    yddhItems.value = getItemsFn("ydxs5", yddhArr.value);
    yydtItems.value = getItemsFn("zxyy6", yydtArr.value);
    sygjItems.value = getItemsFn("zh7", sygjArr.value);
    xxjyItems.value = getItemsFn("hhxx9", xxjyArr.value);
    bgscItems.value = getItemsFn("ppt9", bgscArr.value);
    // console.log(res.data);
    titleList.value = data;
  }

  getUrlDataFn()
  return {
    titleList, xztjArr, cyzdArr, cyzdItems, zyssArr, rjyxArr, rjyxItems, yddhArr, yddhItems, yydtArr, yydtItems, sygjArr, sygjItems, xxjyArr, xxjyItems, bgscArr, bgscItems, getUrlDataFn,getItemsFn,getUrlFn
  }
})